/*
 * @Author: XuYang 
 * @Date: 2021-05-06 17:19:40 
 * @Last Modified by: XuYang
 * @Last Modified time: 2021-05-06 17:37:33
 */
import { Provide } from '@midwayjs/decorator';
import { IWebMiddleware, IMidwayWebNext } from '@midwayjs/web';
import { Context } from 'egg';

@Provide()
export class errorMiddleware implements IWebMiddleware {
    resolve() {
        return async (ctx: Context, next: IMidwayWebNext) => {
            try {
                await next();
            } catch (err) {
                if (err.name === 'UnauthorizedError') {
                    ctx.body = {
                        code: 401,
                        mssage: '请登录后再进行操作',
                    }
                }
            }
        };
    }

}